package com.itheima.Synchronized;

public class Account {
    private String cardId;
    private double money;

    public void getmoney(double money) {
        //获取当前线程的对象
        Thread t = Thread.currentThread();
        //选中代码，按住ctrl+alt+t选择synchronized包裹
        //java中所以对象，都可以作为一把锁
        //实例方法用this锁，不影响其他调用方法
        //静态方法建议用：类名.class，直接锁整个类
        synchronized (this) {

            if (this.money >= money) {
                this.money -= money;
                System.out.println(t.getName() + "正在取钱：。。。。。");
                System.out.println(t.getName() + "取钱成功！余额为：" + this.money);
            } else {
                System.out.println(t.getName() + "取钱失败！余额不足！");
            }
        }
    }


    public Account() {
    }

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "cardId='" + cardId + '\'' +
                ", money=" + money +
                '}';
    }
}

